//****************************************************************
//  PP12_1PalindromeTester.java   Author: 唐才铭
//
//  演示使用递归判断字符串是否为回文。
//****************************************************************
import java.util.Scanner;

public class PP12_1PalindromeTester
{
        public static void main(String[] args )
        {
            String str, another = "y";
            int left, right;

            Scanner scan = new Scanner(System.in);

            while (another.equalsIgnoreCase("y"))  //  allows y or Y
            {
                System.out.println("Enter a potential palindrome: ");
                str = scan.nextLine();

                boolean bool = ishuiwen(str,0,str.length()-1);

                System.out.println();
                if (bool==false)
                    System.out.println("That string is NOT a palindrome");
                else
                    System.out.println("That string IS a palindrome");

                System.out.println();
                System.out.println("Test another palindrome (y/n)?");
                another = scan.nextLine();
            }
        }

        static boolean ishuiwen(String str,int left,int right)
        {
            if (str.length()==0||str.length()==1)
                return true;
            else
                if (left>=right)
                    return true;
                else
                    if (str.charAt(left)==str.charAt(right)) {
                        return ishuiwen(str,left+1,right-1);
                }
                    else
                        return false;
        }
    }
